import $ from 'jQuery';
(function () {
    /**
     * 删除账户
     * @param params
     * @private
     */
    var _deleteGroups = function (params) {
        IOT.confirm('你确定要删除吗？', () => {
            IOT.showOverlay('保存中，请稍等...');
            IOT.getServerData(URI.OPERATE.GROUPS.DELETE, params, function (ret) {
                IOT.hideOverlay();
                if (ret && ret.code === 200) {
                    IOT.tips('删除成功！', 'success');
                    M.Table.refresh.all();
                    BootstrapDialog.closeAll();
                } else {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error');
                }
            });
            return false;
        }, null, 'warn');
    };
    let $vue = new Vue({
        el: '.main-hook',
        data: {
            leaseType: [{id: 0, name: '分组编号'}, {id: 1, name: '分组名称'}],
            boxs: {
                box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
                box2: {show: false, hook: '.box2-hook', url: '', params: {}},
                box3: {show: false, hook: '.box3-hook', url: '', params: {}},
                box4: {show: false, hook: '.box4-hook', url: '', params: {}}
            }
        },
        created: function () {
            //获取所属项目
            //获取维护人
        },
        methods: {
            emitPage: function () {
                M.Page.load(this.boxs);
            },
            // 上一页
            prePage: function () {
                M.Page.prePage(this.boxs);
            },
            // 触发刷新页(当前显示的页面)
            refreshPage: function () {
                M.Page.refreshPage(this.boxs);
            },
            searchKey:function(){
                $('.search-hook').trigger('click');
            },
            // 新建分组
            createGroups: function () {
                M.Page.emit(URI.OPERATE.GROUPS.CREATE.PAGE);
                //IOT.setSessionStore(URI.OPERATE.GROUPS.CREATE.PAGE, JSON.stringify(rowData));
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                let $tableHookName = $('.groups-table-hook');
                let $accountBox = $('.groups-hook');
                //初始化时间插件
                modal.initDate("startdate","enddate");
                var $form = $accountBox.find('.form-hook');
                // 查询
                $form.off('submit').on('submit', function () {
                    $tableHookName.bootstrapTable('onCustomSearch');
                    return false;
                });
                var $tableHook = $tableHookName.bootstrapTable({
                    scrollbar: 'groups-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.OPERATE.GROUPS.LIST,
                    debug: false,
                    cache: false,
                    maintainSelected:true,//设置为 true 在点击分页按钮或搜索按钮时，将记住checkbox的选择项
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    // fixedColumns: true,
                    customButton: '',
                    customQueryParams: function (params) {
                        var formParams = $form.serializeJson();
                        var queryParams = $.extend({}, params, formParams);
                        return queryParams;
                    },
                    onLoadSuccess: function (data) {
                        //console.log(JSON.stringify(data));
                    },
                    onLoadError: function (status, xhr) {
                    },
                    columns: [
                        {
                            field: 'groupCode', title: '分组编号', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'groupName', title: '分组名称', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'proName', title: '所属项目', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'lightCnt', title: '单灯数', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'userName', title: '维护人', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'groupNote', title: '备注', align: 'center',fixedLeft: true,tips:true,
                            /*formatter: function (val, rowData, index) {
                                return val === 'enable' ? '启用' : '停用';
                            }*/
                        },
                        {
                            field: 'cTime', title: '创建时间', align: 'center', fixedLeft: true,tips:true,
                            formatter: function (val, row, index) {
                                return val;
                            }
                        },
                        {
                            field: 'operate', title: '操作', align: 'center', fixedLeft: true,width: 180,
                            events: {
                                'click .detail-groups': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.GROUPS.DETAIL.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.GROUPS.DETAIL.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .update-groups': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.GROUPS.UPDATE.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.GROUPS.UPDATE.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .delete-groups': function (e, value, row, index) {
                                    _deleteGroups(row);
                                    return false;
                                }
                            },
                            formatter: function (value, row, index) {
                                let html = [];
                                if (M.Authority.checkAuthority('account-detail')) {
                                    html.push('<a class="detail-groups operate">查看</a>');
                                }
                                if (M.Authority.checkAuthority('account-modify')) {
                                    html.push('<a class="update-groups operate">修改</a>');
                                }
                                if (!row.admin) {
                                    if (M.Authority.checkAuthority('account-delete')) {
                                        html.push('<a class="delete-groups operate">删除</a>');
                                    }
                                }
                                return html.join('');
                            }
                        }
                    ]
                });
                //权限——查看
                if (!M.Authority.checkAuthority('account-detail')&&!M.Authority.checkAuthority('account-modify')) {
                    $tableHookName.bootstrapTable('hideColumn', 'operate');//隐藏列
                }
            });
        }
    });
})();